/**
 * 
 */
package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;

import domain.Article;
import domain.Computer;
import domain.User;

/**
 * @author grupo22
 *
 */
public class JDBCUserDAO implements IUserDAO {

	/* (non-Javadoc)
	 * @see data.IUserDAO#selectAllUsers()
	 */
	@Override
	public Collection selectAllUsers() {
		Collection result = new LinkedList();
		
		Connection con = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
        	String sql = "SELECT * FROM User;";
        	stmt = con.prepareStatement(sql);
            
            rs = stmt.executeQuery();

            while (rs.next()) {
            	String profile = rs.getString("profile");
            	String id = rs.getString("id");
            	String password = rs.getString("password");
            	String name = rs.getString("name");
                User u = new User(profile, id, password, name);
                result.add(u);
            }
        } catch (SQLException e) {
            System.out.println("Message: Fallo en selectAllUsers " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(con);
        	try {
                    if (rs != null)
                            rs.close();
                    if (stmt != null)
                            stmt.close();
            } catch (SQLException e) {}
        }

		return result;
	}
	
	public User select(Connection con, String oid) {
		String sql = "SELECT * FROM User WHERE (OIDUser = ? )";
        PreparedStatement stmt = null;
        ResultSet rs = null;
        User u = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);
            
            rs = stmt.executeQuery();

            rs.next();
            u = new User(rs.getString("profile"), rs.getString("id"),rs.getString("password"),rs.getString("name"));
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
        return u;
	}
	
	public String selectOID(Connection con, String id) {
		String sql = "SELECT OIDUser FROM User WHERE (id = ? )";
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String s = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, id);

            rs = stmt.executeQuery();

            rs.next();
            s = rs.getString("OIDUser");

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
        return s;
	}

}
